/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年6月7日
 */
package com.massyframework.beanskin.springboot.beancontext.freemarker;

import java.io.IOException;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import com.massyframework.beanskin.assembly.Assembly;
import com.massyframework.beanskin.assembly.AssemblyConfig;

import freemarker.template.TemplateException;

/**
 * Freemarker 配置
 * @author huangkh
 *
 */
@Configuration
public class FreeMarkerConfig {
	
	/**
	 * 激活Freemarker配置, 值为 true或者false.
	 */
	public static final String ENABLED                         = "freemarker.enabled";

	/* 使用缓存 */
	private static final String CACHE                          = "freemarker.cache";
	/* 字符集 */
	private static final String CHARSET                        = "freemarker.charset";
	private static final String CHARSET_DEFAULT                = "UTF-8";
	/* content类型 */
	private static final String CONTENT_TYPE                   = "freemarker.content-type";
	private static final String CONTENT_TYPE_DEFAULT           = "text/html; charset=UTF-8";
	
	/* 模板文件更新延迟 */
	private static final String TEMPLATE_UPDATE_DELAY          = "freemarker.template_update_delay";
	
	private static final String SUFFIX                         = "freemarker.suffix";
	private static final String SUFFIX_DEFALUT                 = ".ftl";
	
	@Autowired
	private Assembly assembly;

	@Bean
	public ViewResolver viewResolver() {
		AssemblyConfig config = assembly.getConfig();
		FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
		resolver.setCache(false);
		resolver.setViewClass(org.springframework.web.servlet.view.freemarker.FreeMarkerView.class);
		resolver.setRequestContextAttribute("request");
		resolver.setExposeSpringMacroHelpers(true);
		resolver.setExposeRequestAttributes(true);
		resolver.setExposeSessionAttributes(true);
		
		resolver.setCache(Boolean.parseBoolean(config.getParameter(CACHE, "true")));
		resolver.setSuffix(config.getParameter(SUFFIX, SUFFIX_DEFALUT));
		resolver.setContentType(config.getParameter(CONTENT_TYPE, CONTENT_TYPE_DEFAULT));
		return resolver;
	}

	@Bean
	public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
		AssemblyConfig config = assembly.getConfig();
		
		FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
		factory.setTemplateLoaderPaths("classpath:BEANSKIN-INF/templates", "classpath:BEANSKIN-INF/resources/templates");
		factory.setDefaultEncoding(config.getParameter(CHARSET, CHARSET_DEFAULT));
		
		FreeMarkerConfigurer result = new FreeMarkerConfigurer();
		freemarker.template.Configuration configuration = factory.createConfiguration();
		configuration.setClassicCompatible(true);
		result.setConfiguration(configuration);
		Properties settings = new Properties();
		settings.put("template_update_delay", config.getParameter(TEMPLATE_UPDATE_DELAY, "0"));
		settings.put("default_encoding", "UTF-8");
		settings.put("number_format", "0.##########");
		settings.put("datetime_format", "yyyy-MM-dd HH:mm:ss");
		settings.put("classic_compatible", true);
		settings.put("template_exception_handler", "ignore");
		result.setFreemarkerSettings(settings);
		return result;
	}

}
